package com.game.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;
import android.util.Log;
import android.view.Display;

import com.game.basic.BasicActivity;
import com.game.config.Config;

/**保存手机信息*/
public class PhoneInfo {
	public static final int startScreenX_def=0;
	public static final int startScreenY_def=0;
	public static final int endScreenX_def=Config.SCREEN_WIDTH;
	public static final int endScreenY_def=Config.SCREEN_HEIGHT;
	public static final int centerScreenX_def=endScreenX_def/2;
	public static final int centerScreenY_def=endScreenX_def/2;
	public static final int screenWidth_def=Config.SCREEN_WIDTH;
	public static final int screenHeight_def=Config.SCREEN_HEIGHT;
	public static int screenWidthHalf_def=Config.SCREEN_WIDTH/2;
	public static int screenHeightHalf_def=Config.SCREEN_HEIGHT/2;
	
	
	//屏幕坐标点
	public static int startScreenX;
	public static int startScreenY;
	public static int endScreenX;
	public static int endScreenY;
	public static int centerScreenX;
	public static int centerScreenY;
	//屏幕宽,高
	public static int screenWidth;
	public static int screenHeight;
	//屏幕宽,高的一半
	public static int screenWidthHalf;
	public static int screenHeightHalf;
	//屏幕与默认屏幕大小的缩放比例
	public static float scaleX=1;
	public static float scaleY=1;
	public static float scaleLonely=1;
	//是否需要缩放
	public static boolean scale=false;
	//系统版本
	public static float AndroidVerson;
	public static String phoneName;
	//静态实例
	public static PhoneInfo instance;
	public static Display display;
	public static boolean landscape=false;
	
	/**
	 * 初始化*/
	public static PhoneInfo newInstance(Display display,boolean landscape){
		
		Log.i("infos", "手机状态信息-------------------------");
		
		PhoneInfo.display=display;
		
		PhoneInfo.landscape=landscape;
		
		//设置屏幕信息,默认为真实的屏幕大小
		setFullScreen();
		//计算缩放比
		if(screenWidth!=Config.SCREEN_WIDTH){
			scaleX=(float)screenWidth/(float)Config.SCREEN_WIDTH;
			scale=true;
		}
		if(screenHeight!=Config.SCREEN_HEIGHT){
			scaleY=(float)screenHeight/(float)Config.SCREEN_HEIGHT;
			scale=true;
		}
		//取比较小的缩放值
		scaleLonely=(PhoneInfo.scaleX-PhoneInfo.scaleY)>0?PhoneInfo.scaleY:PhoneInfo.scaleX;
		
		Log.i("infos", "缩放比宽:"+scaleX+"缩放比高:"+scaleY);
		Log.i("infos", "非全屏的缩放比例"+scaleLonely);
		
		instance=new PhoneInfo();
		
		Log.i("infos", "手机状态信息-------------------------");
		
		logPhoneInfo(BasicActivity.instance);
		
		return instance;
		
		
	}
	
	
	public static void logPhoneInfo(Activity activity) {  
       TelephonyManager mTm = (TelephonyManager) activity.getSystemService(Activity.TELEPHONY_SERVICE);  
       String imei = mTm.getDeviceId();  
       String imsi = mTm.getSubscriberId();  
       String mtype = android.os.Build.MODEL; // 手机型号  
       String numer = mTm.getLine1Number(); // 手机号码，有的可得，有的不可得  
       Log.i("infos","手机型号:"+mtype);
       Log.i("infos","系统版本:"+Build.VERSION.SDK_INT+" "+Build.VERSION.RELEASE);
       Log.i("infos","手机内存:"+getTotalMemory(activity));
       WifiManager wifiManager = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);  
       WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
       Log.i("infos", "mac地址:" + wifiInfo.getMacAddress());  
	}  
	  
    public static String getTotalMemory(Activity activity) {  
		  String str1 = "/proc/meminfo";// 系统内存信息文件  
		  String str2;  
		  String[] arrayOfString;  
		  long initial_memory = 0;  
		  try {  
			  FileReader localFileReader = new FileReader(str1);  
			  BufferedReader localBufferedReader = new BufferedReader(    localFileReader, 8192);  
			  str2 = localBufferedReader.readLine();// 读取meminfo第一行，系统总内存大小  
//			  while((str2=localBufferedReader.readLine())!=null){
//				  Log.i("infos", "str2:::"+str2);
//			  }
			  arrayOfString = str2.split("\\s+");  
			  initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存，单位是KB，乘以1024转换为Byte       
			  localBufferedReader.close();  
		  } catch (IOException e) {   
		  }  
		  return Formatter.formatFileSize(activity.getApplicationContext(), initial_memory);// Byte转换为KB或者MB，内存大小规格化 
    }
	
	/**
	 * 缩放手机屏幕,按比例缩放*/
	public static void notFullScreen(){
		screenWidth=(int)(Config.SCREEN_WIDTH*scaleLonely);
		screenHeight=(int)(Config.SCREEN_HEIGHT*scaleLonely);
		screenWidthHalf=display.getWidth()/2;
		screenHeightHalf=display.getHeight()/2;
		startScreenX=(display.getWidth()-screenWidth)/2;
		startScreenY=(display.getHeight()-screenHeight)/2;
		endScreenX=startScreenX+screenWidth;
		endScreenY=startScreenY+screenHeight;
		centerScreenX=startScreenX+screenWidth/2;
		centerScreenY=startScreenY+screenHeight/2;
		Log.i("infos", "屏幕宽:"+screenWidth+"屏幕高:"+screenHeight);
		Log.i("infos", "起始坐标x:"+startScreenX+"起始坐标y:"+startScreenY);
		Log.i("infos", "结束坐标x:"+endScreenX+"结束坐标y:"+endScreenY);
	}
	
	/**
	 * 将手机屏幕信息设置为真实值,不按比例缩放*/
	public static void setFullScreen(){
		//获得屏幕大小,并计算其一半
		screenWidth=display.getWidth();
		screenHeight=display.getHeight();
		//防止错读屏幕信息
		if(landscape){
			if(screenWidth<screenHeight){
				int temp=screenWidth;
				screenWidth=screenHeight;
				screenHeight=temp;
			}
		}
		screenWidthHalf=screenWidth/2;
		screenHeightHalf=screenHeight/2;
		startScreenX=0;
		startScreenY=0;
		endScreenX=screenWidth;
		endScreenY=screenHeight;
		centerScreenX=startScreenX+screenWidth/2;
		centerScreenY=startScreenY+screenHeight/2;
		Log.i("infos", "屏幕宽:"+screenWidth+"屏幕高:"+screenHeight);
		Log.i("infos", "起始坐标x:"+startScreenX+"起始坐标y:"+startScreenY);
		Log.i("infos", "结束坐标x:"+endScreenX+"结束坐标y:"+endScreenY);
	}
	
	/*重设屏幕*/
	public static void resetScreen(){
		if(Config.FULL_SCREEN){
			setFullScreen();
		}else{
			notFullScreen();
		}
	}
}